iT邦幫忙

2022 iThome 鐵人賽

DAY 7
0
  • 建立集合
  • 取得集合長度
  • 確認集合是否有某一項目
  • 新增(adding)項目到集合中
  • 從集合中移除(removing)項目
  • 清除(clearing)集合
  • 刪除(deleting)集合
  • 轉換(converting)串列為集合
  • 結合(joining)集合
  • 找出交集(intersection)的項目
  • 確認子集(subset)與超集合(super set)
  • 兩集合的差集(difference)
  • 兩集合的對稱差集(symmetric difference)
  • 確認併查集(disjoint)

這篇文章是閱讀Asabeneh的30 Days Of Python: Day 7 - Sets後的學習筆記與心得。


集合中的元素是無順序(unordered)及無索引(un-indexed)的,並且集合中的項目是無法被修改(change)的。

JavaScript(以下簡稱JS)中也有類似的物件類型 - Set

在Python中這種資料型別會用來儲存具唯一性(unique)的項目(items),並能用來尋找聯集(union)、交集(intersection),差集(difference)以及對稱差集(symmetric difference),子集(subset),超集合(super set)和併查集(disjoin set)。

建立集合

  • 使用大括弧{}或是set()函式來建立一個新的空集合
empty_set = {}
empty_set = set()
  • 帶有起始值的集合:
characters = {"a", "b", "c"}

取得集合長度

使用len()函式:

* 原文是寫方法(method),但官方文件中是放在Build-in Functions

characters = {"a", "b", "c"}
len(characters) # 3

確認集合是否有某一項目

與串列及元組相同,使用in運算符:

characters = {"a", "b", "c"}
print("a" in characters) # True
print("p" in characters) # False

新增(adding)項目到集合中

  • 使用add(item)新增一個項目:
characters = {"a", "b", "c"}
characters.add("d")
print(characters) # {'d', 'a', 'b', 'c'}
  • 使用update(item1, item2...)新增多個項目:
characters = {"d", "a", "b", "c"}
characters.update("e", "f", "g")
print(characters) # {'a', 'b', 'e', 'd', 'g', 'f', 'c'}

從集合中移除(removing)項目

使用remove(item)方法,如果該項目不存在於集合中,將產生KeyError

characters = {'a', 'b', 'e', 'd', 'g', 'f', 'c'}
characters.remove("a")
print(characters) # {'b', 'e', 'd', 'g', 'f', 'c'}
characters.remove("k") # KeyError

使用discard(item)方法,如果該項目不存在也不會產生Error

characters = {'a', 'b', 'e', 'd', 'g', 'f', 'c'}
characters.discard("a")
print(characters) # {'e', 'f', 'b', 'd', 'c', 'g'}
characters.discard("k") # nothing happens

使用pop()方法,移除隨機的一個集合項目,並會回傳該移除的項目,如果集合為空,產生KeyError

characters = {'e', 'f', 'b', 'd', 'c', 'g'}
print(characters.pop()) # e (it's random)
print(characters) # {'f', 'b', 'd', 'c', 'g'}

清除(clearing)集合

使用clear(set),把集合內的項目都移除:

characters = {'f', 'b', 'd', 'c', 'g'}
characters.clear()
print(characters) # set()

刪除(deleting)集合

使用del運算符:

characters = set()
del characters

轉換(converting)串列為集合

使用set(list)函式,若串列中原有重複的元素,轉換後只會保留一個,即集合中不會有重複的項目:

polls = ["hiking", "swimming", "hiking", "fitting"]
options = set(polls)
print(options) # {'hiking', 'fitting', 'swimming'}

結合(joining)集合

使用union(set)方法,會回傳一個新的集合:

weekend = {"sunday", "saturday"}
workday = {"monday", "tuesday", "wednesday", "thursday", "friday"}
week = workday.union(weekend)
print(week) # {'thursday', 'friday', 'tuesday', 'wednesday', 'saturday', 'sunday', 'monday'}
print(workday) # {'thursday', 'friday', 'tuesday', 'wednesday', 'monday'}

使用update(set)方法:

weekend = {"sunday", "saturday"}
workday = {"monday", "tuesday", "wednesday", "thursday", "friday"}
workday.update(weekend) # weekend days are added to workday (oh bother!)
print(workday) # {'thursday', 'friday', 'tuesday', 'wednesday', 'saturday', 'sunday', 'monday'}

找出交集(intersection)的項目

使用intersection方法,回傳兩個集合中皆有的項目:

strong = {"java", "c#", "python"}
static = {"c#", "java"}
print(strong.intersection(static)) # {'c#', 'java'}

確認子集(subset)與超集合(super set)

使用issubset(set)方法,回傳True如果set是某集合的子集,否則回傳False
使用issuperset(set)方法,回傳True如果set是某集合的超集合,否則回傳False

week = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}
workday = {"monday", "tuesday", "wednesday", "thursday", "friday"}

# week contains all items in the workday
week.issuperset(workday) # True

# content of workday just a part of the week
week.issubset(workday) # False
workday.issubset(week) # True

兩集合的差集(difference)

使用difference(set)方法,回傳set與某集合的差集:

rgba = {"red", "green", "blue", "alpha"}
hlsa = {"hue", "lightness", "saturation", "alpha"}
print(rgba.difference(hlsa)) # {'green', 'red', 'blue'} - the result is unordered
  • 數學上的表示方法:(rgba\hlsa)

兩集合的對稱差集(symmetric difference)

使用symmetric_difference(set)方法,回傳set與某集合的對稱差集:

rgba = {"red", "green", "blue", "alpha"}
hlsa = {"hue", "lightness", "saturation", "alpha"}
print(rgba.symmetric_difference(hlsa)) # {'green', 'saturation', 'lightness', 'red', 'hue', 'blue'}
  • 數學上的表示方法:(rgba\hlsa) ∪ (hlsa\rgba)

確認併查集(disjoint)

使用isdisjoint,回傳True如果兩集合沒有任何相同的項目,否則回傳False

rgba = {"red", "green", "blue", "alpha"}
hlsa = {"hue", "lightness", "saturation", "alpha"}
rgba.isdisjoint(hlsa) # False - have 'alpha' in common

rgb = {"red", "green", "blue"}
rgb.isdisjoint(hlsa) # True

上一篇
【Day 6】元組
下一篇
【Day 8】 字典
系列文
從前端角度看30天學Python30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言